<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
   %>
     <%@ page import = "java.util.*"
		import = "java.net.*"
		import = "java.io.*" 
		import = "javax.xml.parsers.DocumentBuilder"
		import = "javax.xml.parsers.DocumentBuilderFactory"

		import = "org.w3c.dom.Document"
		import = "org.w3c.dom.Node"
		import = "org.w3c.dom.NodeList"
		
		%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>naverSearchResult.jsp</title>
</head>
<body>

<%
	String s ="http://openapi.naver.com/search?key=c47c0d0ea6c82ae85d13dd2de735b344&target=shop&sort=sim&display=10&query=";
	request.setCharacterEncoding("UTF-8");
	String query = request.getParameter("query");
	//한글의 경우 인코딩이 되서 넘어옵니다
	String url = URLEncoder.encode(query,"UTF-8");
	s+=url;
	
	//다시 디코딩
	String dQuery=URLDecoder.decode(query, "EUC-KR");
	
class Data{//상품의 데이터
	String title;
	String link;
	String image;
	String lprice;
	String hprice;
	String productId;
	String productType;
	
}

		try{
			//s = "http://openapi.naver.com/search?key=c47c0d0ea6c82ae85d13dd2de735b344&target=shop&sort=sim&display=10&query=%EC%9E%90%EC%A0%84%EA%B1%B0";
			URL myurl = new URL(s);//문자를 URL로 변환
			InputStream is = myurl.openStream();//요청보내고 응답을 받는다. 시간이 필요
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();//문서공장
			DocumentBuilder builder = factory.newDocumentBuilder();//문저 쟁이
			Document document = builder.parse(is);//문저쟁이님 저 응답을 해석해주세요.
			
			//전체문서중에<location>태그를 모두 가져오기
			NodeList locationList= document.getElementsByTagName("channel");
			Node searchNode=locationList.item(0);//맨앞에거 가져와(춘천정보)
			
			//춘천 location의 하위 태그들 모두
			NodeList mysearch = searchNode.getChildNodes();
			
			ArrayList<Data> dataList= new ArrayList<Data>();//춘천의 1주일 날씨
			for(int i = 0 ; i<mysearch.getLength(); i++){
				//태그 네임을 가져와라
				String nodeName = mysearch.item(i).getNodeName();
				if(nodeName.equals("item")){//<data>...</data>
					//<data>태그마다 data객체 1개씩
					Data data = new Data();
					
					//<dtat>의 자식들을 가져와라
					NodeList dataChildList = mysearch.item(i).getChildNodes();
					for(int j = 0; j<dataChildList.getLength();j++){
						//<data>의 자식 하나	
						Node dataChildeNode = dataChildList.item(j);
						//<data>의 자식중에 #text는 제외
						if(! dataChildeNode.getNodeName().equals("#text")){
							String value = dataChildeNode.getFirstChild().getNodeValue();
							
							if(dataChildeNode.getNodeName().equals("title")){
								data.title=value;								
							}
							else if(dataChildeNode.getNodeName().equals("image")){
								data.image=value;
							}
							else if(dataChildeNode.getNodeName().equals("lprice")){
								data.lprice=value;
							}
							else if(dataChildeNode.getNodeName().equals("hprice")){
								data.hprice=value;
							}
							else if(dataChildeNode.getNodeName().equals("productId")){
								data.productId=value;
							}
							else if(dataChildeNode.getNodeName().equals("productType")){
								data.productType=value;
							}
							else if(dataChildeNode.getNodeName().equals("link")){
								data.link=value;
								dataList.add(data);
							}
						}
					}
				}
			}
			%><table border = "3" bordercolor="#00BFFF">
				
			<tr>
			<td>번호</td>
			<td>상품명</td><td>주소</td>
			<td>최저가</td><td>최고가</td>
			<td>procuctId</td><td>productType</td>
			<td>이미지</td>
			</tr>
			
			<%
			for(int i = 1; i<dataList.size();i++){
				%><tr>
				<td><%=i%></td>
					<td><%=dataList.get(i).title%></td>
					<td><%%><a href ="<%=dataList.get(i).link%>">링크</a></td>
					<td><%=dataList.get(i).lprice%></td>
					<td><%=dataList.get(i).hprice%></td>
					<td><%=dataList.get(i).productId%></td>
					<td><%=dataList.get(i).productType%></td><br>
					<td><img src ="<%=dataList.get(i).image%>"width = "100"></td><br><%
									
			}%></tr></table><%
				}catch(Exception e){
			
		}
%>



</body>
</html>